/**********************************************************************************************/
/* vsV2_W2U.cpp                                                      						  */
/*                                                                       					  */
/* Copyright Paradigma, 1993-2005															  */
/* All Rights Reserved                                                   					  */
/**********************************************************************************************/

#include "vsV2_W2U.h"

/**********************************************************************************************/
FBL_Begin_Namespace


/**********************************************************************************************/
vsV2_wx2St::vsV2_wx2St( const wxString& inWStr )
{
	Convert( inWStr );
}


/**********************************************************************************************/
void vsV2_wx2St::Convert( const wxString& inWStr )
{
	wxMBConvUTF16 converter;

    // copy the input bytes into a null terminated buffer
	size_t wideChars = inWStr.length();
    wxWCharBuffer inputCopy( wideChars + 1 );
    memcpy( inputCopy.data(), inWStr.c_str(), (wideChars*sizeof(wchar_t)) );
    inputCopy.data()[wideChars] = 0;

    // calculate the output size
    size_t outputWritten = converter.WC2MB( 0, (const wchar_t*)inputCopy.data(), 0);

    // convert the string
    size_t guardBytes = 8; // to make sure we're not overrunning the output buffer
    size_t outputBufferSize = outputWritten + 2 + guardBytes;
    mUStr = new wxCharBuffer(outputBufferSize);

    outputWritten = converter.WC2MB( mUStr->data(), (const wchar_t*)inputCopy.data(), outputBufferSize );
}


/**********************************************************************************************/
vsV2_wx2St::~vsV2_wx2St()
{
	delete mUStr;
}

/**********************************************************************************************/
String vsV2_wx2St::u_str( void )
{
	return String( (const UChar*)mUStr->data() );
}


/**********************************************************************************************/
vsV2_St2wx::vsV2_St2wx( const String& inStr )
{
	Convert( inStr );
}


/**********************************************************************************************/
void vsV2_St2wx::Convert( const String& inStr )
{
	wxMBConvUTF16 converter;

	size_t multibytes = inStr.length() * 2;
    // copy the input bytes into a null terminated buffer
	wxCharBuffer inputCopy( multibytes + 2 );
    memcpy( inputCopy.data(), inStr.c_str(), multibytes );
    memset( &(inputCopy.data())[multibytes], 0, 2 );

    // calculate the output size
    size_t outputWritten = converter.MB2WC( 0, (const char*)inputCopy.data(), 0 );
 
    // convert the string
    size_t guardChars = 8; // to make sure we're not overrunning the output buffer
    size_t nullCharacters = 1;
    size_t outputBufferChars = outputWritten + nullCharacters + guardChars;
	mWStr = new wxWCharBuffer( outputBufferChars );

    outputWritten = converter.MB2WC( mWStr->data(), (const char*)inputCopy.data(), outputBufferChars );
}

/**********************************************************************************************/
vsV2_St2wx::~vsV2_St2wx()
{
	delete mWStr;
}


/**********************************************************************************************/
wxString vsV2_St2wx::wx_str( void )
{
	return wxString(* mWStr );
}


/**********************************************************************************************/
FBL_End_Namespace


